from sqlalchemy import ForeignKey
from sqlalchemy_serializer import SerializerMixin

from apps.ext import db
from apps.model.base import BaseModel

class fmesUsersModel(BaseModel,SerializerMixin):
    """
    出库api表
    """
    __tablename__ = 'fmes_users'
    id = db.Column(db.String(100), primary_key=True, autoincrement=False, doc='id')
    username = db.Column(db.String(125), nullable=False, unique=True, doc='用户名')
    password = db.Column(db.String(125), nullable=False, doc='用户密码')
    user_type = db.Column(db.Integer, nullable=False, default=0, doc='用户类型 1超级管理员，0普通用户')
    is_page = db.Column(db.Boolean, nullable=False, default=False, doc='是否启用分页')
    page_size = db.Column(db.Integer, nullable=False, default=100, doc='分页大小')
    def __str__(self):
        return '用户表:{}'.format(self.username)
    def getListAll(self, parameters: dict, parameters1: dict, page: int = 1, per_page: int = 50):
        try:
            modelObj = self.__class__.query.filter(self.__class__.username.like('%{username}%'.format(username=parameters["username"]))).paginate(page, per_page, False)
            result = self.toList(modelObj.items)
        except Exception as e:
            return None, repr(e)
        objData = {}
        objData["total"] = modelObj.total  # 总记录数
        objData["has_next"] = modelObj.has_next  # 是否有下一页
        objData["has_prev"] = modelObj.has_prev  # 是否有上一页
        objData["page"] = modelObj.page  # 当前页
        objData["pages"] = modelObj.pages  # 总页数
        objData["page_size"] = per_page  # 分页大小
        objData["result"] = result  # 分页大小
        return objData, None



class txUsersModel(BaseModel,SerializerMixin):
    """
    用户表
    """
    __tablename__ = 'xt_users'
    id = db.Column(db.String(100), primary_key=True, autoincrement=False, doc='id')
    username = db.Column(db.String(125), nullable=False, unique=True, doc='用户名')
    password = db.Column(db.String(125), nullable=False, doc='用户密码')
    user_type = db.Column(db.Integer, nullable=False, default=0, doc='用户类型 1超级管理员，0普通用户')

    def __str__(self):
        return '用户表:{}'.format(self.username)
    def getListAll(self, parameters: dict, parameters1: dict, page: int = 1, per_page: int = 50):
        try:
            modelObj = self.__class__.query.filter(self.__class__.username.like('%{username}%'.format(username=parameters["username"]))).paginate(page, per_page, False)
            result = self.toList(modelObj.items)
        except Exception as e:
            return None, repr(e)
        objData = {}
        objData["total"] = modelObj.total  # 总记录数
        objData["has_next"] = modelObj.has_next  # 是否有下一页
        objData["has_prev"] = modelObj.has_prev  # 是否有上一页
        objData["page"] = modelObj.page  # 当前页
        objData["pages"] = modelObj.pages  # 总页数
        objData["page_size"] = per_page  # 分页大小
        objData["result"] = result  # 分页大小
        return objData, None

class txPrintTaskModel(BaseModel,SerializerMixin):
    """
    打印任务表
    """
    __tablename__ = 'xt_printtask'
    id = db.Column(db.String(100), primary_key=True, autoincrement=False, doc='id打印任务id')
    qty = db.Column(db.Integer, nullable=False,  doc='打印数量')
    type = db.Column(db.Integer, nullable=False, doc='打印类型1、生产2、固定资产')
    order = db.Column(db.String(225),  nullable=False, doc='打印单号')
    status = db.Column(db.Boolean,  nullable=False, doc='是否被打印 0未打印、1已打印')
    print = db.Column(db.String(200),  nullable=False, doc='选择的打印机')

    def __str__(self):
        return '打印任务表:{}'.format(self.id)
    # def getListAll(self, parameters: dict, parameters1: dict, page: int = 1, per_page: int = 50):
    #     try:
    #         modelObj = self.__class__.query.filter(self.__class__.username.like('%{username}%'.format(username=parameters["username"]))).paginate(page, per_page, False)
    #         result = self.toList(modelObj.items)
    #     except Exception as e:
    #         return None, repr(e)
    #     objData = {}
    #     objData["total"] = modelObj.total  # 总记录数
    #     objData["has_next"] = modelObj.has_next  # 是否有下一页
    #     objData["has_prev"] = modelObj.has_prev  # 是否有上一页
    #     objData["page"] = modelObj.page  # 当前页
    #     objData["pages"] = modelObj.pages  # 总页数
    #     objData["page_size"] = per_page  # 分页大小
    #     objData["result"] = result  # 分页大小
    #     return objData, None

class txPrintFacilityModel(db.Model):
    """
    打印设备记录表
    """
    __tablename__ = 'xt_printfacility'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='ID')
    name = db.Column(db.String(125), nullable=False, unique=True, doc='打印机名称')
    def __str__(self):
        return '打印设备:{}'.format(self.id)


class txRkRecordModel(BaseModel,SerializerMixin):
    """
    入库记录表
    """
    __tablename__ = 'xt_rkrecord'
    id = db.Column(db.String(100), primary_key=True, autoincrement=False, doc='id--转移单号')
    qty = db.Column(db.DECIMAL, nullable=False,  doc='转移单数量---报检数量')
    c_qty = db.Column(db.DECIMAL, nullable=False, default=0,  doc='输入时超出的数量')
    cz_name = db.Column(db.String(200),  nullable=True, doc='操作人')
    lr_name = db.Column(db.String(200),  nullable=True, doc='检验人--录入人')
    bat_no = db.Column(db.String(200),  nullable=True, doc='批号')
    rem = db.Column(db.Text,  nullable=True, doc='备注')
    def __str__(self):
        return '入库记录表:{}'.format(self.id)